package dao.Impl;

import bean.Manager;
import bean.Menu;
import dao.MenuDao;
import util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DruidUtil implements MenuDao {
    @Override
    public Menu findByMid(int mid) {
        List<Manager> managerList = new ArrayList<>();
        Menu menu = new Menu();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement("select * from menu m,manager man, mid where m.mid = mid.mid and man.manid = mid.manid and m.mid = ?");
            preparedStatement.setInt(1,mid);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                //1.分别获取信息
                menu.setMname(resultSet.getString("mname"));
                Manager manager = new Manager();
                manager.setManname(resultSet.getString("manname"));
                //关联信息
                managerList.add(manager);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(connection,preparedStatement,resultSet);
        }
        menu.setManagerList(managerList);
            return menu;
    }

    @Override
    public Manager findByManid(int manid) {
        List<Menu> menuList = new ArrayList<>();
        Manager manager = new Manager();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement("select * from menu m,manager man, mid where m.mid = mid.mid and man.manid = mid.manid and man.manid = ?");
            preparedStatement.setInt(1,manid);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                //1.分别获取信息
                Menu menu = new Menu();
                menu.setMname(resultSet.getString("mname"));
                manager.setManname(resultSet.getString("manname"));
                //关联信息
                menuList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(connection,preparedStatement,resultSet);
        }
        manager.setMenuList(menuList);
        return manager;
    }
}
